################################
# Figure 2
################################
library(dplyr)
library(readstata13)

#2006
price06 = readxl::read_xlsx("price_06_house.xlsx")
price06$Contract = stringr::str_trim(price06$Contract)   
price06$inc_win = ifelse(price06$Contract %in% c("RH.hold06","RH.gain06"),1,0) 
data06 =as.data.frame(tapply(price06$LastPrice, list(price06$Date, price06$inc_win), sum, na.rm=T))
colnames(data06) = c("inc_lose","inc_win") 
data06$inc_lose=ifelse(data06$inc_lose>1, 1,data06$inc_lose)
inc_lose = data06[1:160,]$inc_lose
names(inc_lose)=row.names(data06[1:160,]) 
par(mar=c(3.7,5.1,1,1))
plot(inc_lose, type="l",ylim=c(0,1),axes=F,xlab="",ylab="",lwd=1.5);box() 
xseq<-seq.Date(as.Date("2006-06-01"),as.Date("2006-11-07"),by='1 day') 
n = length(row.names(data06)) /15
s =  c(1, 15*1:n)
row.names(data06)[s]
axis(1, at=s, labels=FALSE)
text(x=s, y=-0.1,
     labels=format(as.Date(row.names(data06)[s]),"%m/%d"), srt=45,adj=1, xpd=T,cex=1.7)
axis(side=2,at=seq(0,1,by=0.1), labels=seq(0,1,by=0.1),las=2, cex.axis=1.3) #ADD Y-AXIS LABELS
mtext('Date',side=1,line=5) #X-AXIS LABEL
mtext('Pr(Incumbent Lose)',side=2,line=3.5, cex=2.2) #Y-AXIS LABEL
abline(h=0.5, col="red",lty=2,lwd=2)


#2010
price10 = readxl::read_xlsx("price_10_house.xlsx")
price10$Contract = stringr::str_trim(price10$Contract)   
price10$inc_win = ifelse(price10$Contract %in% c("DH.gain10","DH.hold10"),1,0) 
data10 =as.data.frame(tapply(price10$LastPrice, list(price10$Date, price10$inc_win), sum, na.rm=T))
colnames(data10) = c("inc_lose","inc_win")
data10$inc_lose[44]=(0.39+0.425)/2
data10$inc_lose[80]=(0.43+0.403)/2
data10 = data10[1:298,]

data10$action_date = row.names(data10) 
data10$action_date = format(as.Date(data10$action_date, format = "%Y-%m-%d"), "%y-%m-%d") 
data10$inc_lose=ifelse(data10$inc_lose>1, 1,data10$inc_lose)
inc_lose = data10[1:296,]$inc_lose
names(inc_lose)=row.names(data10[1:296,]) 
par(mar=c(3.7,5.1,1,1))
plot(inc_lose, type="l",ylim=c(0,1),axes=F,xlab="",ylab="",lwd=1.5);box() 
xseq<-seq.Date(as.Date("2010-10-01"),as.Date("2010-11-02"),by='1 day') 
n = length(row.names(data10)) /31
s =  c(1, 31*1:n)
row.names(data10)[s]
axis(1, at=s, labels=FALSE)
text(x=s, y=-0.1,
     labels=format(as.Date(row.names(data10)[s]),"%m/%d"), srt=45, adj=1, xpd=T,cex=1.7)
axis(side=2,at=seq(0,1,by=0.1), labels=seq(0,1,by=0.1),las=2,cex.axis=1.3) #ADD Y-AXIS LABELS
mtext('Date',side=1,line=5) #X-AXIS LABEL
mtext('Pr(Incumbent Lose)',side=2,line=3.5, cex=2.2) #Y-AXIS LABEL
abline(h=0.5, col="red",lty=2,lwd=2)

#2017-2018
price18 = readxl::read_xls("price_18_senate.xls")
price18$Contract = stringr::str_trim(price18$Contract)  
price18$inc_win = ifelse(price18$Contract %in% c("RS.gain18","RS.hold18"),1,0) 
price18$Date =format(as.Date(price18$Date,format="%Y-%m-%d"), "%y-%m-%d") 
data18 =as.data.frame(tapply(price18$LastPrice, list(price18$Date, price18$inc_win), sum, na.rm=T))
colnames(data18) = c("inc_lose","inc_win")
inc_lose = data18$inc_lose
names(inc_lose)=row.names(data18) 

data18$inc_lose=ifelse(data18$inc_lose>1, 1,data18$inc_lose)
inc_lose = data18[1:579,]$inc_lose
names(inc_lose)=row.names(data18[1:579,])
inc_win= data18$inc_win
names(inc_win)=row.names(data18)
par(mar=c(3.5,5.1,1,1)) 
plot(inc_lose, type="l",ylim=c(0,1),axes=F,xlab="",ylab="",lwd=1.5);box() 
n = length(row.names(data18)) /60
s =  c(1, 60*1:n)
row.names(data18[1:579,])[s]
axis(1, at=s, labels=FALSE)
text(x=s, y=-0.1,
     labels=format(as.Date(row.names(data18)[s]),"%m/%d"), srt=45, adj=1, xpd=T,cex=1.7)
axis(side=2,at=seq(0,1,by=0.1), labels=seq(0,1,by=0.1),las=2,cex.axis=1.3) #ADD Y-AXIS LABELS
mtext('Date',side=1,line=5) #X-AXIS LABEL
mtext('Pr(Incumbent Lose)',side=2,line=3.5, cex=2.2) #Y-AXIS LABEL
abline(h=0.5, col="red",lty=2,lwd=2)


